package lib.db.pool;

import inet.util.Encrypter;
import inet.util.StringTool;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

public class MySQLGetConnection{
	public static final String fileName = "D://config//mysql_config.conf";
	//public static final String fileName = "conf/mysql_config.conf";
	//private static final String fileName = MySQLGetConnection.class.getResource("mysql_config.conf").getFile();
	public static Hashtable<String, ConnectionParameters> loadData() {
        //Vector v = new Vector();
		Hashtable<String, ConnectionParameters> temp  =null;
		ConnectionParameters parameters = null;
        try {
            //BufferedReader in = new BufferedReader(new FileReader("conf/mysql_config.conf"));
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            String line = in.readLine();
            for (; line != null; line = in.readLine()) {
                line = line.trim();                
                if ("".equals(line) || line.startsWith("#")) {
                    continue;
                }
                //System.out.println(line);
                int idx = line.indexOf("=");
                if (idx <= 0) {
                    System.out.println("Seperator '=' is NOT found.");
                    continue;
                }
                String name = line.substring(0, idx).trim();
                String value = line.substring(idx + 1).trim();
                
                if (name == null || name.equals("") ||
                    value == null || value.equals("")) {
                	continue;
                }                
                
                parameters = new ConnectionParameters();

                Collection c = StringTool.parseString(value, ",");
                
                Iterator it = c.iterator();                                
                parameters.setDriver( (String) it.next());                
                parameters.setUrl( (String) it.next());
                parameters.setUser( (String) it.next());
                if(it.hasNext())                	
	                try {
	                    parameters.setPassword(Encrypter.decrypt((String) it.next()));
	                } catch (Exception ex) {
	                    System.out.println("   DBGetConnetion: invalid encrypted password = " + String.valueOf(it.next()));
	                    ex.printStackTrace();
	                }
                else
                	parameters.setPassword("");                                
                temp = new Hashtable<String, ConnectionParameters>();
                temp.put(name, parameters);
               // System.out.println(temp.get("xoso"));
                //setHtblPool(temp);
            }            
        } catch (Exception ex) {
            ex.printStackTrace();
        }       
        return temp;
    }
	public static void main(String [] args) {
		System.out.println(Encrypter.encrypt("123456"));
	}
}
class ConnectionParameters {
    private String driver;
    private String url;
    private String user;
    private String password;//clear text
    //private int count; //number of connections

    public void setDriver(String s) {
        this.driver = s;
    }
    public void setUrl(String s) {
        this.url = s;
    }
    public void setUser(String s) {
        this.user = s;
    }
    public void setPassword(String s) {
        this.password = s;
    }    

    public String getDriver() {
        return this.driver;
    }
    public String getUrl() {
        return this.url;
    }
    public String getUser() {
        return this.user;
    }
    public String getPassword() {
        return this.password;
    }   
    public String toString() {
        return "<" + getDriver() + ">,<" + getUrl() +  ">,<" + getUser()+ ">,<***>";
    }
}
